package com.mr.biz.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.mr.biz.ProductRepository;
import com.mr.biz.support.AbstractRepository;
import com.mr.biz.support.Request;
import com.mr.biz.support.Response;
import com.mr.dao.entity.product.Product;
import com.mr.dao.service.EntityService;
import com.mr.utils.OgnlUtils.Parser;

/**
 * @author Alex
 *
 */
@Repository
public class ProductRepositoryImpl extends AbstractRepository implements ProductRepository {

	@Resource
	private EntityService<Product> entityService;

	@Override
	public Response loadByCategory(Request request) throws Exception {
		Parser parser = getRequestParser(request);
		String id = parser.parse("id", String.class);
		List<Product> products = entityService.find(Product.class, "category.id", id);

		Map<String, Object> root = createRoot();
		root.put("products", products);
		root.put("category", products.get(0).getCategory());
		return generateResponseWithFreeMarker(root, "ProductRepository-loadByCategory");
	}
}
